<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;

use App\Models\Addressapi;

class mapIntoSql extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'map-into-sql';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '将全国省市区地址保存到数据库';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $n=0;
        $addressmodel = new Addressapi();
        $data = file_get_contents('https://restapi.amap.com/v3/config/district?keywords=&subdistrict=3&key=c8149a3da34eaa5a1de69a34e1a42412');
        $data = json_decode($data, true);
        $area_arr = $data['districts'];
        foreach ($area_arr as $b) {
            foreach ($b['districts'] as $c) {
                foreach ($c['districts'] as $d) {
                    foreach ($d['districts'] as $e) {

                        if (is_array($e['citycode'])) {
                            $e['citycode'] = "";
                        }
                        $saveData = [
                            'adcode' => $e['adcode'],
                            'name' => $e['name'],
                            'center' => $e['center'],
                            'level' => $e['level'],
                            'city_code' => $e['citycode'],
                        ];

                        //  print_r($saveData);

                        $n=$n+1;

                        $addressmodel->saveData($saveData);

                        echo $n;
                    }
//                  if(is_array($d['citycode'])){
//                      $d['citycode']="";
//                  }
//                  $saveData = [
//                  'adcode'=>$d['adcode'],
//                  'name' => $d['name'],
//                  'center'=>$d['center'],
//                  'level' => $d['level'],
//                  'city_code' =>$d['citycode'],
//              ];
//                  $addressmodel->saveData($saveData);
//                  echo "d";
//              }

                }
            }

            //return $area_arr;


        }

    }


}
